package com.zelin.gulimall.product.service.impl;

import com.zelin.gulimall.product.vo.AttrAttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zelin.common.utils.PageUtils;
import com.zelin.common.utils.Query;

import com.zelin.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.zelin.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.zelin.gulimall.product.service.AttrAttrgroupRelationService;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }
    //移除属性与分组的关联关系
    @Override
    public void relationDelete(AttrAttrGroupVo[] vo) {
        //1.得到要删除的关联集合对象
        List<AttrAttrgroupRelationEntity> collect = Arrays.asList(vo).stream().map(r -> {
            AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(r, entity);
            return entity;
        }).collect(Collectors.toList());
        //2.批量删除
        System.out.println("collect = " + collect);
        this.baseMapper.delRalationDelete(collect);
    }

    //添加属性与分组的关联关系
    @Override
    public void add(List<AttrAttrGroupVo> vo) {
        List<AttrAttrgroupRelationEntity> entities = new ArrayList<>();
        for (AttrAttrGroupVo groupVo : vo) {
            AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(groupVo,entity);
            entities.add(entity);
        }
        //批量添加
        this.saveBatch(entities);
    }

}